package blue.qingyu.whimurmur.enhancement.directive;

import java.util.List;

import com.jfinal.aop.Inject;
import com.jfinal.template.Env;
import com.jfinal.template.io.Writer;
import com.jfinal.template.stat.Scope;

import io.jboot.db.model.Column;
import io.jboot.db.model.Columns;
import io.jboot.web.directive.annotation.JFinalDirective;
import io.jboot.web.directive.base.JbootDirectiveBase;
import io.jpress.module.article.model.ArticleComment;
import io.jpress.module.article.service.ArticleCommentService;

@JFinalDirective("articleCommentEx")
public class ArticleCommentExDirective extends JbootDirectiveBase {
	@Inject
	private ArticleCommentService articleCommentService;
	
	@Override
	public void onRender(Env env, Scope scope, Writer writer) {
		Integer commentCount = getParaToInt("count", scope, 5);
		String orderBy = getPara("orderBy", scope, "created desc");
		String column = getPara("column", scope, "1=1");
    	List<ArticleComment> articleComments = articleCommentService.findListByColumns(Columns.create(Column.create(column)), orderBy, commentCount);
		scope.setLocal("comments", articleComments);
		
		Long totalCount = articleCommentService.findCountByStatus("normal");
		scope.setLocal("commentCount", totalCount);
        renderBody(env, scope, writer);

	}
	
	@Override
    public boolean hasEnd() {
        return true;
    }
}
